package net.togogo.controller;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HiController {

    @ResponseBody
    @RequestMapping("/hi")
    public String hi(){
        return "你好啊，qdd";
    }

    //用户登录
    @GetMapping("/login")
    public String login(){
        //登录token判断 若token内含用户信息 则无需再次输入密码 直接跳转到指定路径
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth instanceof AnonymousAuthenticationToken){
            return "login";
        }
        return "index";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    //获取登录用户的信息
    @GetMapping("/info")
    @ResponseBody
    public Object getUserInfo(Authentication authentication){
        return authentication;
    }

    //用户退出
    @GetMapping("/logout")
    public String logout(){
        return "out";
    }
}
